<template>
  <div class="login-style">
    <el-form
      ref="loginFormRef"
      :model="loginForm"
      :rules="rules"
      label-width="80px"
      status-icon
      class="login-form"
      @keyup.enter.native="onLoginBtn()"
    >
      <h1 class="title">登录</h1>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="loginForm.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="loginForm.password" placeholder="请输入密码" type="password"></el-input>
      </el-form-item>
      <div class="login-btn">
        <el-button type="primary" @click="onLoginBtn()">登录</el-button>
        <el-button @click="onResetBtn()">重置</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
// import { routes } from '@/router/routes';
export default {
  name: 'LoginPage',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 3, max: 15, message: '用户名长度为3-15' },
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 3, max: 15, message: '密码长度为3-15' },
        ],
      },
    };
  },
  created() {},
  methods: {
    onLoginBtn() {
      // sessionStorage.setItem('userInfo', JSON.stringify(this.loginForm));
      // this.$router.push({ path: '/' }).catch((err) => {
      //         console.log(err);
      //       });
      this.$refs['loginFormRef'].validate((valid) => {
        if (valid) {
          this.$store
            .dispatch('Login', this.loginForm)
            .then(() => {
              this.$message.success('登录成功');
              this.$router.push({ path: '/' }).catch((err) => {
                console.log(err);
              });
            })
            .catch((err) => console.log(err));
        }
      });
    },
    onResetBtn() {
      this.$refs.resetPsdRef.resetFields();
    },
  },
};
</script>

<style scoped>
.login-style {
  width: 100vw;
  height: 100vh;
  background-image: url('../../assets/login.png');
  background-size: 100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-form {
  width: 350px;
  background-color: #fff;
  padding: 20px;
  padding-top: 0;
}

.title {
  text-align: center;
  margin: 20px 0;
}

.login-btn {
  display: flex;
  justify-content: center;
}
</style>
